<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Order_m extends CI_Model {
	function __construct()
    {
        parent::__construct();
    }

	function saveexpress()
	{
	    $postdata="";
		//YT 云途   YW 燕文
        $express_type=array('sfexpress'=>1,'UPS'=>2,'DHL'=>3,'CNE'=>4,'UBI'=>5,'YT'=>6,'YW'=>7,'EMS'=>8,'WUY'=>9);
		$orderlist = trim($this->input->post('orderlist'));
		if(!$orderlist)
		{
			$this->fun->phpalert(2, "请输入订单号");
			die;
		}
        $post_express_type=$this->input->post('type');
        $orderType=$express_type[$post_express_type];
		//如果是无忧物流，需要存诸相关配置的信息
		if($orderType == 9)
		{
			$wuyconfig=""; 
			$wuyconfig["pickstyle"] = $this->input->post('pickstyle');	//上门揽收或自行配送
			$wuyconfig["kd"] = $this->input->post('kd');	//国内快递
			$wuyconfig["kdnumber"] = $this->input->post('kdnumber');//快递号
			$wuyconfig["Expressqudao"] = $this->input->post('Expressqudao');//线上发货物流
			$wuyconfig["isbattery"] = $this->input->post('isbattery');//是否含电池
			$wuyconfig["UndeliverableDecision"] = $this->input->post('UndeliverableDecision');//不可达处理
			if($wuyconfig){
				$newwuconfig = serialize($wuyconfig);
			}
		}

		
		
		$orderlist = substr($orderlist,0,-1);
		$orderdata = implode("','",explode(",",$orderlist));	//替换字符
		$data = $this->db->get_where("db_order_list","order_sn in ('".$orderdata."')")->result_array();
     
	    $insert_data=array();//存储已经正确的需要做交运的mabang订单id
		
		if(count($data)>0){
			foreach($data as $key=>$rs){
        		//必须都是已配货状态
				/*
        		foreach($data as $key=>$res)
        		{
        			$status[] = $res["order_status"];
        		}
        		$status = array_unique($status);
        		if(count($status)>1) //去重复后，大于1个元素
        		{
        			die("存在不同的订单状态");
        		}else{
        			if($status[0] !=3)
        			{
        				//die("不是配货中状态");
        			}
        		
        		}*/
			     $insert_ids=array();
			     $insert_ids['order_id']=$rs['order_id'];
                 $insert_ids['order_sn']=$rs['order_sn'];
		
				$rsdb = $this->db->get_one("SELECT * FROM db_mabangorder WHERE orderID='".trim($rs["order_sn"])."' AND orderType ='".$orderType."'");
			     //echo $this->db->last_query();
                 //dump($rsdb);
                // die;
            	if($rsdb)
				{
					$thi1orderid = $rsdb["id"];
				}else{
					$thi1orderid = 0;
				}

					if( !$rs["tel"] &&  !$rs["mobile"])
					{
						$rs["tel"] = '0000000000';
					}
					
					switch ($rs["order_status"]) {
						case 1;
							$statustext = '新订单';
							break;
						case 2:
							$statustext  = '已支付';
							break;
						case 3:
							$statustext  = '配货中';
							break;
						case 4:
							$statustext  = '已发货';
							break;
						case 5:
							$statustext  = '已完成';
							break;
						case 6:
							$statustext  = '已作废';
							break;
						case 7:
							$statustext  = '待审查';
							break;
				  }
					$rs['consignee'] =str_replace("&","/",$rs['consignee']);
					$postdata["orderType"] = $orderType;
					$postdata["orderID"] = trim($rs["order_sn"]);
					$postdata["statusst"]=$statustext;
					$postdata["customerid"] = $rs["consignee"];
					$postdata["customertype"]= $rs["shopname"];
					$postdata["customertel1"]= $rs["tel"];
					$postdata["customertel2"]= $rs["mobile"];
					//获取国家标识strtoupper
					if($rs["country_code"] =='')
					{
						$rscountry = $this->fun->getdatabasesdetail("d_country"," AND countrynamecn='".strtoupper(trim($rs["country"]))."'");
						if($rscountry)
						{
							$customerqq = $rscountry["countryid"];
						}
					}else{
						$customerqq = $rs["country_code"];
					}


					$strprovince = strlen($rs["province"]);
					if($strprovince>3 ||  $rs["province"] =='')
					{
						//自动抓取省code
						$resprovince = $this->fun->getdatabasesdetail("d_country_state"," AND name='".addslashes(trim($rs["province"]))."'");
						$province = $resprovince['code'];
					}else{
						$province =strtoupper($rs["province"]);
					}

					$postdata["customerqq"]= $customerqq;	//国家标识
					$postdata["customercountry"]= $rs["country"];//国家
					$postdata["customerprovince"]= $province?$province:trim($rs["province"]);
					$postdata["customercity"]= $rs["city"];
					$postdata["customeraddress"]= $rs["address"].$rs["address2"];
					$postdata["customerzipcode"]= $rs["zipcode"];
					$postdata["moneytype"]= $rs["moneytype"];
					$postdata["wuyconfig"]= $newwuconfig?$newwuconfig:null;
					$postdata["content1"]= NULL;
					$postdata["intime"]= date("Y-m-d H:i:s");
					$postdata["createUser"]=$_COOKIE["username"];
				
					$mabang_id= 0;
					if( $thi1orderid && $rsdb['isexe']!=1)
					{
						$postdata["isexe"]= 0;
						$postdata["exememo"]='重复提交订单';
						$this->db->update('db_mabangorder', $postdata, 'id=' . $thi1orderid);
                        $mabang_id = $insert_ids['mabang_id']=$thi1orderid;
                       
					}else{
						 $res = $this->db->query($this->db->insert_string('db_mabangorder', $postdata));
                         $mabang_id = $insert_ids['mabang_id']=$this->db->insert_id();
					}
					
					if($orderType == 1 || $orderType == 2)
					{

						$address = $rs["address"].$rs["address2"];
						 if(stripos($address,'PO Box')===0)
						{
							$exememo = "收货地址PO Box";
							$postdatast="";
							$postdatast["isexe"] = 2;
							$postdatast["exememo"] = $exememo;
							$postdatast["mailinstime"] =time();
							$this->db->query($this->db->update_string('db_mabangorder', $postdatast, 'id=' .$mabang_id));
						}
					}


					if($orderType==9)
					{
						$postdatast="";
						$shopst = $this->fun->getdatabasesdetail("d_shop"," AND id='".$rs['shopid']."'");
						if($shopst['agent_visit'] ==0)//这两个店铺暂时不使用远程
						{
							
						}else{
							//无忧的交运需要存放在远程
							$rsdb = $this->fun->getdatabasesdetail("db_mabangorder"," AND id='".$mabang_id."'");
							$this->load->library('Amazondata');
							$ress = $this->amazondata->savealiexpresswuyou($rsdb,$rs['shopid'],$rs['tradeid']);
							$postdatast="";
							$postdatast["isexe"]= 2;
							$postdatast["exememo"]='等待远程自动交运完成';
							$this->db->update('db_mabangorder', $postdatast, 'id=' .$mabang_id);
						}
					}

                   // echo  $this->db->last_query();
               // $insert_ids['mabanginfo']=$rsdb;
               // $insert_ids['orderinfo']=$rs;
                $insert_data[]=$insert_ids;
			}
		}	
		return $insert_data;
	}
	
	function jsmergeorder($orderID)
	{
		$orderlist = $this->fun->getdatabaseslist("db_order_list"," AND order_id in($orderID)");
			foreach($orderlist as $key=>$rs)
			{
				$tradeid[] = $rs["tradeid"];
				$shopid[] = $rs["shopid"];
				$consignee[] = strtolower($rs["consignee"]);
				$country[] = strtolower($rs["country"]);
				$address[] = strtolower(trim($rs["address"]));
				$order_snlist[] = $rs["order_sn"];
			}
			
			/*
			$tradeid = array_unique($tradeid);	//去重复之后，如果count=1 说明只有一个订单，重复导单形成
			if(count($tradeid)==1)
			{
				$reat["code"] = 201;
				$reat["txt"] = "合并订单不成功，交易单号相同！说明导单过程中出现重复订单！";
				echo json_encode($reat);
				die;
			}*/

			$shopid = array_unique($shopid);	//去重复
			if(count($shopid)>1)
			{
				$reat["code"] = 201;
				$reat["txt"] = "合并订单不成功，订单来自不同的店铺！";
				echo json_encode($reat);
				die;
			}
			$consignee = array_unique($consignee);	//去重复
			if(count($consignee)>1)
			{
				$reat["code"] = 201;
				$reat["txt"] = "合并订单不成功，来自不同的用户订单！";
				echo json_encode($reat);
				die;
			}
			$country = array_unique($country);	//去重复
			if(count($country)>1)
			{
				$reat["code"] = 201;
				$reat["txt"] = "合并订单不成功，来自不同的国家！";
				echo json_encode($reat);
				die;
			}
			$address = array_unique($address);	//去重复
			if(count($address)>1)
			{
				$reat["code"] = 201;
				$reat["txt"] = "合并订单不成功，来自不同的地址！";
				echo json_encode($reat);
				die;
			}
			if(!$order_snlist)
			{
				$reat["code"] = 201;
				$reat["txt"] = "合并订单不成功，订单信息不存在！";
				echo json_encode($reat);
				die;
			}
			/*******开始合并订单信息*****/
			$orderlist = $this->fun->getdatabaseslist("db_order_list"," AND order_id in($orderID)");
			$ordersnlist = implode(",",$order_snlist);

			//费用进行合并
			$rsOrder = $this->fun->getdatabasesdetail("db_order_list"," AND order_id in($orderID)"," SUM(goods_originask) as goods_originask, SUM(goods_amount) as goods_amount, SUM(shipping_fee) as shipping_fee,SUM(shipping_originask) as shipping_originask,SUM(pay_fee) as pay_fee,SUM(platform_fee) as platform_fee,remark");
			foreach($orderlist as $key=>$rs)
			{
				if($key==0)
				{
					$orderID = $rs["order_id"];
					$ordersn = $rs["order_sn"];
					$remark = "订单由{$ordersnlist}合并而成 ";
					$remark = $rsOrder['remark'].$remark;
					//订单的运费和收入需要加上
					$postdata="";
					$postdata['order_source'] ='合并订单';
					$postdata['remark'] =$remark;
					$postdata['goods_originask'] =$rsOrder['goods_originask'];
					$postdata['goods_amount'] =$rsOrder['goods_amount'];
					$postdata['shipping_fee'] =$rsOrder['shipping_fee'];
					$postdata['shipping_originask'] =$rsOrder['shipping_originask'];
					$postdata['pay_fee'] =$rsOrder['pay_fee'];
					$postdata['platform_fee'] =$rsOrder['platform_fee'];
					$this->db->update("db_order_list",$postdata,"order_id=".$orderID);
				}else{
					$newordersn.= $rs["order_sn"];
					$remark = "订单已合并成{$ordersnlist} ";
					$this->db->update("db_order_list",array("order_status"=>6,"order_source"=>"合并订单","remark"=>$remark),"order_id=".$rs["order_id"]);//设置作废
					$this->db->update("db_order_sell",array("order_id"=>$orderID),"order_id=".$rs["order_id"]);//转移商品信息
					$rval = "合并订单：订单-><a href=\"/orderlist/editorder?sn=$newordersn\">$newordersn</a> 合并到订单<a href=\"/orderlist/editorder?order_id=$orderID\">$ordersn</a>";
					$this->fun->goods_update_log($orderID,$rval,3);	//更新订单信息
					$this->fun->goods_update_log($rs["order_id"],$rval,3);	//更新订单信息
				}
				if($rs['order_status']==7)
				{
					$this->db->update("db_order_list",array("order_status"=>7),"order_id=".$orderID);
				}
			}

			//重新计算总商品重量
			$sell = $this->db->get_where("db_order_sell",array("order_id"=>$orderID))->result_array();
			$orderweight= 0;
			foreach($sell as $key=>$rs)
			{
				$orderweight = $orderweight+($rs["goods_weight"]*$rs["goods_number"]);
			}
			$this->db->update("db_order_list",array("orderweight"=>$orderweight),"order_id=".$orderID);
				
				

			return $orderID;
	}
	function savewuyexpress()
	{
		print_r($_POST);
		DIE;
	}


	function getSearchCountry()
	{

		$depts = $this->fun->getdatabaseslist("d_chinaexpress"," and `OPENFLAG` IS not NULL","id,NAME,FILED1,FILED3,FILED4","group by FILED1");
        foreach ($depts as $key => $rs) {
            $deptsname[$rs["FILED3"]][] = $rs;
        }
	
		return $deptsname;
	}

	function selectShop()
	{
		$depts = $this->fun->getdatabaseslist("d_shop"," and del_flag =0","id,name,platfromname");
        foreach ($depts as $key => $rs) {
            $deptsname[$rs["platfromname"]][] = $rs;
        }
		return $deptsname;
	}
	function selectZipcode()
	{
		$zipcodes = $this->fun->getdatabaseslist("d_zipcode"," and del_flag = 0","id,name,zipcode,type");
		foreach ($zipcodes as $key => $rs) {
			$zipname[$rs["name"]][] = $rs;
		}
		return $zipname;
	}
	function selectLogistics()
	{
		$logistics = $this->fun->getdatabaseslist("d_logistics"," and del_flag = 0","id,name,logisticsName,logisticsValue,type");
		foreach ($logistics as $key => $rs) {
			$logisticsname[$rs["name"]][] = $rs;
		}
		return $logisticsname;
	}
	function saveexpresstype()
	{
		$postdata= "";
		$postdata["name"]    = trim($this->input->post('name'));
		$postdata["aliexpress_express"] = trim($this->input->post('aliexpress_express'));
		$postdata["ebay_express"] = trim($this->input->post('ebay_express'));
		$postdata["amazon_express"] = trim($this->input->post('amazon_express'));
		$postdata["wish_express"] = trim($this->input->post('wish_express'));
		$postdata["magento_express"] = trim($this->input->post('magento_express'));
		$postdata["shopee_express"] = trim($this->input->post('shopee_express'));
		$postdata["oversea_express"] = trim($this->input->post('oversea_express'));
		$postdata["weburl"] = trim($this->input->post('weburl'));
		$postdata["address"] = trim($this->input->post('address'));
		$postdata["opertime"] = date("Y-m-d H:i:s");
		$postdata["oper"] = $_COOKIE["username"];
		$postdata["types"]    = trim($this->input->post('types'));
		$postdata["isopenshpping"] = $this->input->post('isopenshpping');
		$postdata["discount"] = $this->input->post('discount');
		
		if($this->input->post("id"))
		{
			$this->db->update("db_order_express",$postdata,"id=".$this->input->post("id"));
			$intid =intval($this->input->post("id"));
		}else{
			$this->db->insert("db_order_express",$postdata);
			$intid = $this->db->insert_id();
		}

		return true;
	}

	function saveordersearch()
	{
		if($this->input->post('orderstatus'))
		{
			$orderstatus  =implode(",",$this->input->post('orderstatus'));
		}
		$postdata= "";
		$postdata["name"]    = trim($this->input->post('name'));
		$postdata["country"] = trim($this->input->post('country'));
		$postdata["countryflag"] = trim($this->input->post('countryflag'));
		$postdata["shopname"] = trim($this->input->post('shopname'));
		$postdata["shopflag"] = trim($this->input->post('shopflag'));
		$postdata["goodsnoflag"] = trim($this->input->post('goodsnoflag'));
		$postdata["goodsno"] = trim($this->input->post('goodsno'));
		$postdata["weight1"] = trim($this->input->post('weight1'));
		$postdata["weight2"] = trim($this->input->post('weight2'));
		$postdata["amount1"] = trim($this->input->post('amount1'));
		$postdata["amount2"] = trim($this->input->post('amount2'));
		$postdata["freight1"] = trim($this->input->post('freight1'));
		$postdata["freight2"] = trim($this->input->post('freight2'));
		$postdata["orderstatus"] = $orderstatus;
		$postdata["ordertime"] = trim($this->input->post('ordertime'));
		$postdata["logisticsID"] = intval($this->input->post('logisticsID'));
		$rsdb = $this->fun->getdatabasesdetail("db_order_express"," AND id=".intval($this->input->post('logisticsID')));
		$postdata["logistics"] = $rsdb["name"];
		$postdata["priority"] = trim($this->input->post('filed13'));
		$postdata["weightflag"] = trim($this->input->post('weightflag'));
		$postdata["amountflag"] = trim($this->input->post('amountflag'));
		$postdata["freightflag"] = trim($this->input->post('freightflag'));
		$postdata["ordermoneyflag"] = trim($this->input->post('ordermoneyflag'));
		$postdata["isfactory"] = trim($this->input->post('isfactory'));
		$postdata["opertime"] = date("Y-m-d H:i:s");
		$postdata["oper"] = $_COOKIE["username"];
		//发货仓库
		//$postdata["warehouseId"] = trim($this->input->post('deliverWarehouse'));
		if($this->input->post('deliverWarehouse'))
		{
			$postdata["warehouseId"]  =implode(",",$this->input->post('deliverWarehouse'));
		}
		$this->db->trans_begin();
		
			if($this->input->post("id"))
			{
				$this->db->update("db_order_search",$postdata,"id=".$this->input->post("id"));
			    $intid =intval($this->input->post("id"));
			}else{
				$this->db->insert("db_order_search",$postdata);
			    $intid = $this->db->insert_id();
			}
			
			$sql = "";
			if($postdata["country"])	//国家Sql
			{
				$country = explode(";",$postdata["country"]);
				$country = implode("','",$country );
				if($this->input->post("countryflag")==1)
					$sql.= " AND country_cn not in ('".$country."')";
				else
					$sql.= " AND country_cn in ('".$country."')";
			}
			if($postdata["shopname"])	//店铺SQL
			{
				$shopname = explode(";",$postdata["shopname"]);
				$shopname = implode("','",$shopname );

				//$shopid = $this->fun->getdatabasesdetail("d_shop"," AND shopname in ('".$shopname."')"," GROUP_CONCAT(id) as shopid");
				//print_r($shopid);
				//die;
				if($this->input->post("shopflag")==1)
					$sql.= " AND shopname not in ('".$shopname."')";
				else
					$sql.= " AND shopname in ('".$shopname."')";
			}

			if($postdata["goodsno"])	//商品编号SQL
			{
				$goodsno = explode(";",$postdata["goodsno"]);
				$goodsno = implode("','",$goodsno );
				if($this->input->post("goodsnoflag")==1)
					$sql.= " and order_id in(select distinct order_id from db_order_sell where goods_sn not in ('".$goodsno."'))";
				else
					$sql.= " and order_id in(select distinct order_id from db_order_sell where goods_sn  in ('".$goodsno."'))";
			}
			if($postdata['isfactory'])
			{
				if($postdata['isfactory'] ==1)
				{
					$sql.=" AND order_id in(SELECT distinct order_id FROM db_order_sell where length(goods_sn)=8 AND (locate(11,goods_sn)=1 or locate(12,goods_sn)=1)) ";
				}
				if($postdata['isfactory'] ==2)
				{
					$sql.=" AND order_id not in(SELECT distinct order_id FROM db_order_sell where length(goods_sn)=8 AND (locate(11,goods_sn)=1 or locate(12,goods_sn)=1))";
				}
			}
			if($postdata["weight2"])	//包裹重量
			{
				if($postdata["weightflag"] ==1)
				{
					$sql.= " AND orderweight not between $postdata[weight1] and $postdata[weight2] ";
				}else{
					$sql.= " AND orderweight between $postdata[weight1] and $postdata[weight2] ";
				}
			}

			
			if($postdata["amount2"])	//金额SQL
			{
				if($postdata["amountflag"] ==1)
				{
					if($postdata["ordermoneyflag"] ==1)
					{
						$sql.= " AND goods_amount+shipping_fee not between $postdata[amount1] and $postdata[amount2] ";
					}else{
						$sql.= " AND goods_amount not between $postdata[amount1] and $postdata[amount2] ";
					}
				}else{
					if($postdata["ordermoneyflag"] ==1)
					{
						$sql.= " AND goods_amount+shipping_fee between $postdata[amount1] and $postdata[amount2] ";
					}else{
						$sql.= " AND goods_amount between $postdata[amount1] and $postdata[amount2] ";
					}
					
				}
			}
			if( $postdata["freight2"])	//运费SQL
			{
				if($postdata["freightflag"] ==1)
				{
					$sql.= " AND shipping_fee not between $postdata[freight1] and $postdata[freight2] ";
				}else{
					$sql.= " AND shipping_fee between $postdata[freight1] and $postdata[freight2] ";
				}
				
			}

			if($postdata["warehouseId"])
			{
				$warehouseID = $postdata["warehouseId"];
				$sql.= " AND shipping_status in (".$warehouseID.")";
			}
			
			if($orderstatus)	//订单状态SQL
			{
				$sql.= " and order_status in($orderstatus)";
			}
			if($postdata["ordertime"])	//订单时间SQL
			{
					$daynum = $postdata["ordertime"];
					$time = strtotime("-$daynum day");
					$sql.= " and ordertime > date_add(now(), INTERVAL -$daynum day)";
			}

			if($sql)
			{
				$this->db->update("db_order_search",array("sqlfilter"=>$sql),"id=".$intid);
			}
			
			if ($this->db->trans_status() === FALSE)
			{
				$this->db->trans_rollback();
				return false;
			}
			else
			{
				$this->db->trans_commit();
			}
		
		return true;
	}
	
	/****************************
	*** 重新计算总商品价格
	*******************************/
	function recalculategoodsprice($orderID)
	{
		//订单信息
		$rsorder = $this->fun->getdatabasesdetail('db_order_list'," AND order_id ='".$orderID."' ","shopid");
		$rshop = $this->db->get_where("d_shop","id=".intval($rsorder['shopid']))->result_array();
        $rshop=$rshop[0];
		if($rshop["shop_platform_fee"]>0)
			{
				$fee  =$rshop["shop_platform_fee"];
			}else{
				if($rshop['platfromname']=='aliexpress')
				{
					$fee = 0.1;
				}elseif($rshop['platfromname']=='Magento')
				{
					$fee = 0.1;
				}elseif($rshop['platfromname']=='淘宝')
				{
					$fee = 0;
				}elseif($rshop['platfromname']=='Bonanza')
				{
					$fee = 0.09;
				}elseif($rshop['platfromname']=='Tmall')
				{
					$fee = 0.05;
				}elseif($rshop['platfromname']=='Shopee')
				{
					$fee = 0.03;
				}else{
					$fee = 0.15;
				}
		}
		//重新计算总商品价格
		$sell = $this->db->get_where("db_order_sell",array("order_id"=>$orderID))->result_array();
		$goods_amount =$goods_originask=$orderweight= 0;
		$orderup="";
		foreach($sell as $key=>$rs)
		{
			$goods_amount = $goods_amount+$rs["goods_amount"];
			if($rs["originsellprice"]){
				$goods_originask =$goods_originask+($rs["originsellprice"] * $rs['goods_number']);
			}
			$orderweight = $orderweight+($rs["goods_weight"]*$rs["goods_number"]);
		}
		$orderup["goods_amount"] =$goods_amount;
		if($goods_originask>0){
			$orderup["goods_originask"] =$goods_originask;
		}
			
		$orderup["platform_fee"] = $goods_amount*$fee;
        if($rshop['platfromname']=='Bonanza'){//不更新平台交易费
            unset($orderup["platform_fee"]); 
        }
		if($rshop["shop_pay_fee"]>0)
		{
			$fee_pay_fee  =$rshop[0]["shop_pay_fee"];
		}else{
			$fee_pay_fee = 0.015;
		}
		$orderup["pay_fee"] = $goods_amount*$fee_pay_fee;

		if($rshop['platfromname']=='Tmall'){//不更新平台交易费
           $orderup["pay_fee"] =0;
        }
		$orderup["orderweight"] = $orderweight;
		//判断是否超重
		if($orderweight >=2000)
		{
			$orderup["is_overweight"] = 1;
		}else{
			$orderup["is_overweight"] = 2;
		}

		$this->db->update("db_order_list",$orderup,"order_id='".$orderID."'");
	}

	function saveshoseSize()
	{
		$rsdb = $this->fun->getdatabasesdetail('db_order_assist'," AND order_id='".intval($this->input->post('order_id'))."' AND types = 1 AND sellID='".intval($this->input->post('rec_id'))."' ");
			if($rsdb)
			{
				if($this->input->post('custom')==1)	//1均码
				{
					$post["types"]		= 1;
					$post["order_id"]	= intval($this->input->post('order_id'));
					$post["sellID"]		= intval($this->input->post('rec_id'));
					$post["varcha1"]    = $this->input->post('custom');
					$post["varcha2"]    = $this->input->post('shoes_size');
					$post["varcha3"]    = '';
					$post["varcha4"]    ='';
					$post["varcha5"]    = '';
					$post["varcha6"]    = '';
					$post["varcha7"]    = '';
					$post["varcha8"]    = '';
					$post["varcha9"]    = '';
					$post["varcha10"]    ='';
					$post["varcha11"]    = '';
					$this->db->update("db_order_assist",$post,"sid = ".$rsdb['sid']);
					$rval = "鞋子尺码：均码 ".$this->input->post('shoes_size');
					$this->fun->goods_update_log(intval($this->input->post('order_id')),$rval,3);	//更新订单信息
				}else{
					//定制
					$post["types"]		= 1;
					$post["order_id"]	= intval($this->input->post('order_id'));
					$post["sellID"]		= intval($this->input->post('rec_id'));
					$post["varcha1"]    = $this->input->post('custom');
					$post["varcha2"]    = '';
					$post["varcha3"]    = trim($this->input->post('varcha3'));
					$post["varcha4"]    = trim($this->input->post('varcha4'));
					$post["varcha5"]    = trim($this->input->post('varcha5'));
					$post["varcha6"]    = trim($this->input->post('varcha6'));
					$post["varcha7"]    = trim($this->input->post('varcha7'));
					$post["varcha8"]    = trim($this->input->post('varcha8'));
					$post["varcha9"]    = trim($this->input->post('varcha9'));
					$post["varcha10"]    = trim($this->input->post('varcha10'));
					$post["varcha11"]    = trim($this->input->post('varcha11'));
					if($this->input->post('varcha3') && $this->input->post('varcha4'))
					{
						$this->db->update("db_order_assist",$post,"sid = ".$rsdb['sid']);
					}
					
					
					if($this->input->post('varcha3') && $this->input->post('varcha4') && $post["varcha5"] =='' )
					{
						$rval = "鞋子尺码：定制 ".$this->input->post('varcha3')." 鞋码:".trim($this->input->post('varcha4'))." ". $post["varcha11"];
						$this->fun->goods_update_log(intval($this->input->post('order_id')),$rval,3);	//更新订单信息
					}
				}
			
			}else{
				$post="";
				if($this->input->post('custom')==1)	//1均码
				{
					$post["types"]		= 1;
					$post["order_id"]	= intval($this->input->post('order_id'));
					$post["sellID"]		= intval($this->input->post('rec_id'));
					$post["varcha1"]    = $this->input->post('custom');
					$post["varcha2"]    = $this->input->post('shoes_size');
					$post["varcha3"]    = '';
					$post["varcha4"]    ='';
					$post["varcha5"]    = '';
					$post["varcha6"]    = '';
					$post["varcha7"]    = '';
					$post["varcha8"]    = '';
					$post["varcha9"]    = '';
					$post["varcha10"]    ='';
					$post["varcha11"]    = '';
					$this->db->insert("db_order_assist",$post);
					$rval = "鞋子尺码：均码 ".$this->input->post('shoes_size');
					$this->fun->goods_update_log(intval($this->input->post('order_id')),$rval,3);	//更新订单信息
				}else{
					//定制
					$post["types"]		= 1;
					$post["order_id"]	= intval($this->input->post('order_id'));
					$post["sellID"]		= intval($this->input->post('rec_id'));
					$post["varcha1"]    = $this->input->post('custom');
					$post["varcha2"]    = '';
					$post["varcha3"]    = trim($this->input->post('varcha3'));
					$post["varcha4"]    = trim($this->input->post('varcha4'));
					$post["varcha5"]    = trim($this->input->post('varcha5'));
					$post["varcha6"]    = trim($this->input->post('varcha6'));
					$post["varcha7"]    = trim($this->input->post('varcha7'));
					$post["varcha8"]    = trim($this->input->post('varcha8'));
					$post["varcha9"]    = trim($this->input->post('varcha9'));
					$post["varcha10"]    = trim($this->input->post('varcha10'));
					$post["varcha11"]    = trim($this->input->post('varcha11'));
					if($this->input->post('varcha3') && $this->input->post('varcha4'))
					{
						$this->db->insert("db_order_assist",$post);
					}
					if($this->input->post('varcha3') && $this->input->post('varcha4') && $post["varcha5"] =='' )
					{
						$rval = "鞋子尺码：定制  ".$this->input->post('varcha3')."  鞋码:".trim($this->input->post('varcha4'))." ". $post["varcha11"];
						$this->fun->goods_update_log(intval($this->input->post('order_id')),$rval,3);	//更新订单信息
					}
				}
			}
			return true;
	}

	function getorderisok()
	{
		
		$data = $this->db->get_where("db_amzordertmp","status = 2")->result_array();
		if(count($data)>0)
		{
			foreach($data as $key=>$rs)
			{
				
				$orderlist = $this->fun->getdatabaseslist("db_amzordertmpsell"," AND goods_id = 0 AND ordertmpid = '".$rs["id"]."'");
				if(count($orderlist)>0)
				{
					$span = 0;
					foreach($orderlist as $key=>$res){
						$goods = $this->fun->getdatabasesdetail('db_goods'," AND goods_sku = '".$res['productid']."'","goods_id,goods_name,g_weight");
						if($goods)
						{
						
							$postsell="";
							$postsell['productid'] = $res['productid'];
							$postsell['goods_id'] = $goods['goods_id'];
							$postsell['productname'] = $goods['goods_name'];
							$cosprice = $this->fun->get_Goods_costprice($res['productid']);
							$postsell['costprice'] = $cosprice*$res["ordernum"];
							$postsell['goods_weight'] = $goods['g_weight'];
							$derprice = $this->fun->getdatabasesdetail("t_overseaproduct"," AND sku = '".$res['productid']."'","deliverprice");
							if($derprice['deliverprice']>0){
								$postsell['deliverprice'] =$derprice['deliverprice'];//头程费用
							}
							 $this->db->update("db_amzordertmpsell",$postsell," id='".$res['id']."'" );
						}
					}
			   }
				$isnust = $this->fun->getdatabasesdetail("db_amzordertmpsell"," AND goods_id=0 and ordertmpid='".$rs['id']."'","id");
				if(!$isnust)
				{
					$this->db->update("db_amzordertmp",array('status'=>1)," id ='".$rs['id']."' AND status=2");
				}
			}
		}
	}

	function ImportNewexpress($upfile)
	{
		if($resExcel = PHPExcel_IOFactory::load($upfile)){
		$allRow = $resExcel->getActiveSheet()->getHighestRow();

				$s = 0;
				
				for($i=2;$i<=$allRow;++$i){
					$postdata = "";
					$A=trim($resExcel->getActiveSheet()->getCell('A'.$i)->getValue());	//ID
					$A = $this->fun->myTrim($A);
					if($A){
						$postdata["title"]=trim($resExcel->getActiveSheet()->getCell('B'.$i)->getValue());
						$postdata["types"]=trim($resExcel->getActiveSheet()->getCell('C'.$i)->getValue());
						$postdata["area"] =trim($resExcel->getActiveSheet()->getCell('D'.$i)->getValue());	//大分类
						$postdata["expresstypeid"] =trim($resExcel->getActiveSheet()->getCell('E'.$i)->getValue());	//小分类
						$postdata["baseweight"]=trim($resExcel->getActiveSheet()->getCell('F'.$i)->getValue());	//成本价
						$postdata["basemoney"]=trim($resExcel->getActiveSheet()->getCell('G'.$i)->getValue());	//商品重量
						$postdata["unitweight"]=trim($resExcel->getActiveSheet()->getCell('H'.$i)->getValue());	//采购商家
						$postdata["unitmoney"]=trim($resExcel->getActiveSheet()->getCell('I'.$i)->getValue());	//采购链接
						$postdata["reserve6"]=trim($resExcel->getActiveSheet()->getCell('J'.$i)->getValue());	//采购天数
						$postdata["reserve7"]=trim($resExcel->getActiveSheet()->getCell('K'.$i)->getValue());	//库存警戒天数
						$postdata["country"]=trim($resExcel->getActiveSheet()->getCell('L'.$i)->getValue());	//零售价格
					
					}
					
					$this->db->insert("db_order_express_formula",$postdata);

					
				
				
				}
			}
	}

}
